package br.pb.sistcomp.dao;

import br.pb.sistcomp.entity.Leilao;
import br.pb.sistcomp.exception.ErroAoRemoverException;
import br.pb.sistcomp.exception.ErroAoSalvarException;
import java.util.List;
import javax.persistence.EntityManager;
import br.pb.sistcomp.util.HibernateUtil;

/**
 * Class that responsible by manager the database
 *
 * @author Renan Oliveira
 * @version 1.01
 *
 * Copyright (C) 2011 SistComp.
 */
public class LeilaoDaoImpl implements LeilaoDao {

    private EntityManager em;

    /**
     * 
     *Construct responsible by create one instance of EntityManager 
     * 
     */
    public LeilaoDaoImpl() {

        em = HibernateUtil.getInstance().getFactory().createEntityManager();

    }

    /**
     *
     * Method responsible by add one Leilao in database
     * @param one object Leilao
     * @throws ErroAoSalvarException
     * 
     */
    @Override
    public void addLeilao(Leilao leilao) throws ErroAoSalvarException {

        try {

            em.getTransaction().begin();
            em.persist(leilao);
            em.getTransaction().commit();

        } catch (Exception e) {

            em.getTransaction().rollback();

            throw new ErroAoSalvarException();

        }


    }

    /**
     *
     * Method responsible by remove one Leilao of database
     * 
     * @return one Object Leilao
     * @throws ErroAoRemoverException
     */
    @Override
    public Leilao removeLeilao(Leilao leilao) throws ErroAoRemoverException {

        try {

            em.getTransaction().begin();
            em.remove(leilao);
            em.getTransaction().commit();

            return leilao;

        } catch (Exception e) {

            em.getTransaction().rollback();

            throw new ErroAoRemoverException();


        }
    }

    /**
     *
     * Method responsible by list all Leilao of database 
     * @return one list of object Leilao of database
     * 
     */
    @Override
    public List<Leilao> listarTodos() {
        return em.createQuery("SELECT c FROM Leilao c").getResultList();
    }

    /**
     *
     * Method responsible by list all Leilao active of database 
     * @return one list of object Leilao of database
     * 
     */
    @Override
    public List<Leilao> listarTodosAtivos() {

        return em.createQuery("SELECT c FROM Leilao c WHERE status = 'Ativo'").getResultList();


    }

    /**
     * 
     * Method responsible by list all Leilao inactive of database 
     * @return one list of object Leilao of database
     * 
     */
    @Override
    public List<Leilao> listarTodosInativos() {
        return em.createQuery("SELECT c FROM Leilao c WHERE status = 'Inativo'").getResultList();

    }

    /**
     *
     * Method responsible by list all Leilao of database 
     * @return one list of object Leilao of database
     * 
     */
    //TODO implement the list is missing one hour
    @Override
    public List<Leilao> listarFaltaUmaHora() {
        return em.createQuery("SELECT c FROM Leilao c").getResultList();
    }

    /**
     *
     * Method responsible by search Leilao by id
     * 
     * @param one variable int
     * @return one object Leilao of database
     * 
     */
    @Override
    public Leilao procurar(int id) {

        return em.find(Leilao.class, id);


    }
}
